package com.wts.client;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.wts.client.https.HttpUtils;
import com.wts.client.utils.StringToBytes;
import com.wts.client.utils.WtcJsonMap;

/**
 * 免密登录|集群同步相关
 * 
 * @author macpl
 *
 */
public class WtsClient {
	private String secret;
	private String operatorLoginname;
	private String operatorPassword;
	private String baseUrl;

	public WtsClient(String baseUrl, String secret, String operatorLoginname, String operatorPassword) {
		super();
		this.secret = secret;
		this.baseUrl = baseUrl;
		this.operatorLoginname = operatorLoginname;
		this.operatorPassword = operatorPassword;
	}

	/**
	 * 服务是否可用
	 * 
	 * @return
	 */
	public boolean isLive(boolean throwExceptionAble) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("loginname", operatorLoginname);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/auth/get/user.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			if (throwExceptionAble) {
				throw new RuntimeException(backData.toString());
			} else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 查询系统参数
	 * 
	 * @param keyPart 部分key，模糊匹�?
	 */
	public String getParameter(String key) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("key", key);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/auth/get/parameter.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
		WtcJsonMap json = new WtcJsonMap(backData.toMap());
		return json.getString("DATA");
	}

	/**
	 * 获得子系统的访问地址
	 * 
	 * @param string
	 * @return
	 */
	public String getUrlByPop(String loginname, String homeUrl) {
		String url = "";
		if (homeUrl.trim().toUpperCase().indexOf("HTTP") == 0) {
			url = homeUrl;
		} else {
			url = baseUrl + homeUrl;
		}
		String certificate = null;
		if (StringUtils.isNotBlank(loginname)) {
			Map<String, String> data = new HashMap<String, String>();
			data.put("secret", secret);
			data.put("operatorLoginname", operatorLoginname);
			data.put("operatorPassword", operatorPassword);
			// -----------
			data.put("loginname", loginname);
			JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/auth/regist/login.do", data);
			if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
				throw new RuntimeException(backData.toString());
			}

			certificate = backData.getString("CERTIFICATE");
		}
		if (url.indexOf("?") > 0) {
			return url + "&LOGIN_CERTIFICATE=" + certificate;
		} else {
			return url + "?LOGIN_CERTIFICATE=" + certificate;
		}
	}

	/**
	 * 免登录地址生成，为链接添加免登录凭证
	 * 
	 * @param loginname
	 * @param homeUrl
	 * @param appendBaseUrl 是否凭借接口的baseURl
	 * @return
	 */
	public String getUrlByPop(String loginname, String homeUrl, boolean appendBaseUrl) {
		String url = "";
		if (!appendBaseUrl) {
			url = homeUrl;
		} else {
			url = baseUrl + homeUrl;
		}
		String certificate = null;
		if (StringUtils.isNotBlank(loginname)) {
			Map<String, String> data = new HashMap<String, String>();
			data.put("secret", secret);
			data.put("operatorLoginname", operatorLoginname);
			data.put("operatorPassword", operatorPassword);
			// -----------
			data.put("loginname", loginname);
			JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/auth/regist/login.do", data);
			if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
				throw new RuntimeException(backData.toString());
			}

			certificate = backData.getString("CERTIFICATE");
		}
		if (url.indexOf("?") > 0) {
			return url + "&LOGIN_CERTIFICATE=" + certificate;
		} else {
			return url + "?LOGIN_CERTIFICATE=" + certificate;
		}

	}

	/**
	 * 判断是否负载服务节点
	 * 
	 * @param throwExceptionAble
	 * @return
	 */
	public boolean isLiveShareNode(boolean throwExceptionAble) {
		try {
			if (isLive(throwExceptionAble)) {
				Map<String, String> data = new HashMap<String, String>();
				data.put("secret", secret);
				data.put("operatorLoginname", operatorLoginname);
				data.put("operatorPassword", operatorPassword);
				// -----------
				JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/isShareNode.do", data);
				if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
					if (throwExceptionAble) {
						throw new RuntimeException(backData.toString());
					} else {
						return false;
					}
				} else {
					return true;
				}
			} else {
				return false;
			}
		} catch (Exception e) {
			if (throwExceptionAble) {
				throw new RuntimeException(e);
			} else {
				return false;
			}
		}
	}

	/**
	 * 设置负载服务节点
	 * 
	 * @param serverurl
	 * @param serverSecret
	 * @param apiLoginname
	 * @param apiPassword
	 */
	public void setShareNode(String serverurl, String serverSecret, String apiLoginname, String apiPassword,
			String serverhome) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("serverurl", serverurl);
		data.put("serverSecret", serverSecret);
		data.put("apiLoginname", apiLoginname);
		data.put("apiPassword", apiPassword);
		data.put("serverhome", serverhome);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/setShareNode.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
	}

	/**
	 * 清理數據
	 */
	public void clearData() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/clear.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
	}

	/**
	 * 清理全部缓存
	 */
	public void clearCaches() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/clearcache.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
	}

	/**
	 * 獲得附件对象的json(不含文件本身)
	 * 
	 * @param fileid
	 * @return
	 */
	public String getDocFile(String fileid) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("fileid", fileid);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/getDocFile.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
		String hexString = backData.getString("JSON");
		return StringToBytes.hex2Bytes(hexString);
	}

	/**
	 * 刪除负载答题室
	 * 
	 * @param roomid
	 */
	public void removeShareRoom(String roomid) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("roomid", roomid);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/removeRoom.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
	}

	/**
	 * 同步表数据
	 * 
	 * @param jsonHexStr json的byte编码 用StringToBytes編碼
	 * @param domainName
	 */
	public void sycnTable(Object sharebeanObj, String sharebean) {
		String jsonHexStr = StringToBytes.bytes2Hex(com.alibaba.fastjson.JSONObject.toJSONString(sharebeanObj));
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("jsonHexStr", jsonHexStr);
		data.put("sharebean", sharebean);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/table.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
	}

	/**
	 * 同步刪除表数据
	 * 
	 * @param cardid
	 * @param wtsCard
	 */
	public void sycnRemoveCard(String cardid) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		data.put("cardid", cardid);
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/delcard.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
	}

	/**
	 * 集群节点数据统计
	 * 
	 * @return
	 */
	public Map<String, Integer> getNodeNums() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("secret", secret);
		data.put("operatorLoginname", operatorLoginname);
		data.put("operatorPassword", operatorPassword);
		// -----------
		JSONObject backData = HttpUtils.httpPost(baseUrl + "/api/sycn/nums.do", data);
		if (!backData.has("STATE") || backData.getInt("STATE") != 0) {
			throw new RuntimeException(backData.toString());
		}
		JSONObject nums = backData.getJSONObject("NUMS");
		Map<String, Integer> numsmap = new HashMap<String, Integer>();
		for (String key : nums.keySet()) {
			numsmap.put(key, nums.getInt(key));
		}
		return numsmap;
	}

}
